package com.xiaojie.learn.util.threadpool;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class NamedThreadFactory implements ThreadFactory {

    private final AtomicInteger poolNumber = new AtomicInteger(1);

    private final AtomicInteger threadNumber = new AtomicInteger(1);

    public final String namePrefix;

    public NamedThreadFactory(String namePrefix) {
        if(null == namePrefix || "".equals(namePrefix.trim())){
            namePrefix = "pool";
        }
        this.namePrefix = namePrefix + "-" + poolNumber.getAndIncrement() + "-";
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(namePrefix+threadNumber.getAndIncrement());
        return t;
    }
}
